package org.takinframework.web.quartz;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.takinframework.core.quartz.QuartzManager;
import org.takinframework.core.quartz.TriggerTime;
import org.takinframework.core.util.LogUtil;
import org.takinframework.core.util.ResourceUtil;
import org.takinframework.web.buss.job.BrowserJob;
import org.takinframework.web.buss.job.DepotJob;
import org.takinframework.web.system.service.SystemService;

import com.google.common.collect.Lists;
/**
 * spring 初始化后需要执行的类
 * @author twg
 *
 */
@Component
public class InstantiationQuartzProcessor implements
		ApplicationListener<ContextRefreshedEvent> {
	
	@Autowired
	private SystemService systemService;
	
	private boolean isStart = false;
	
	public void startJob() {
		if(!isStart){//没有父容器时说明它是最大的
			isStart = true;
			LogUtil.debug("=======定时任务开始启动=======");
			String jobName = "粮仓智能通风";
			String description = "按每天【小时:分钟】执行一次";
			String date[] = {"10:10:00","10:38:00","10:55:00","13:25:00",
					"13:52:00","14:25:00","14:55:00","15:25:00","15:55:00",
					"16:25:00","16:55:00","17:25:00","17:55:00","18:25:00"};
			List<TriggerTime> triggerTimes = Lists.newLinkedList();
			for (int i = 0; i < date.length; i++) {
				TriggerTime time = new TriggerTime();
				time.setTimeType(3);
				time.setTimeInterval(date[i]);
				time.setPriority(i);
				triggerTimes.add(time);
			}
			QuartzJob(jobName, triggerTimes, description);
			//////////////////////////////////////////////////
			jobName = "检测浏览器是否激活";
			description = "按每天【每分钟】执行一次";
			List<TriggerTime> browserTriggerTime = Lists.newLinkedList();
			TriggerTime time = new TriggerTime();
			time.setTimeType(2);
			time.setTimeInterval(ResourceUtil.getConfig("sessionExpire"));//检测浏览器是否激活
			browserTriggerTime.add(time);
			BrowserJob(jobName, browserTriggerTime, description);
			
		}
	}
	
	private void QuartzJob(String jobName, List<TriggerTime> triggerTimes,String description){
		try {
			QuartzManager.addJob(jobName, DepotJob.class, triggerTimes, description);
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 服务器段检测浏览器是否激活(用于控制同一用户只能登陆一个)
	 * @param jobName 任务名
	 * @param triggerTimes 定时器
	 * @param description  描述
	 */
	private void BrowserJob(String jobName, List<TriggerTime> triggerTimes,String description){
		try {
			QuartzManager.addJob(jobName, BrowserJob.class, triggerTimes, description);
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}


	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getParent() == null)
		startJob();
	}

}
